const {getUser, register} = require('../../utils/request')
Page({

    login() {
        wx.getUserProfile({
            desc: '用来完善资料',
            success: res => {
                const openid = wx.getStorageSync('openid')
                getUser(openid).then(r => {
                    if (r.data.length > 0) {
                        //如果存在直接从数据库获取数据
                        wx.setStorageSync('userInfo', r.data[0])
                        wx.setStorageSync('logged', true)
                        wx.navigateBack({
                            delta: 0,
                        })
                    } else {
                        //没有则注册用户信息
                        register(res.userInfo.avatarUrl, res.userInfo.nickName, res.userInfo.gender).then(() => {
                            wx.setStorageSync('userInfo', res.userInfo)
                            wx.setStorageSync('logged', true)
                            wx.navigateBack({
                                delta: 0,
                            })
                        }).catch(err => {
                            wx.setStorageSync('logged', false)
                            console.log(err)
                        })
                    }
                })
            }
        })
    },
    cancel() {
        wx.navigateBack({
            delta: 1,
        })
    },
    onLoad(options) {

    },

})